package experiment5;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class No_1 {
    public static void main(String []args){

        Course math=new Course("math");

        //测试各函数的功能
        math.addStudent("w");
        math.addStudent("z");
        System.out.println(math.getStudents()[0]);
        System.out.println(math.getStudents()[1]);
        System.out.println(math.getNumberOfStudents());
        System.out.println(math.getCourseName());
        math.dropStudent("w");
        System.out.println(math.getNumberOfStudents());
    }
}

class Course{
    private String courseName;

    //初始化数组大小为0
    private String[] students = new String[0];

    //初始化学生数为0
    private int numberOfStudents=0;

    public Course(String courseName) {
        this.courseName = courseName;
    }

    //通过生成新数组实现增加元素
    public void addStudent(String student) {
        String[] temp=new String[students.length+1];
        numberOfStudents++;
        System.arraycopy(students,0,temp,0,students.length);
        temp[temp.length-1]=student;
        students=temp;
    }

    public void dropStudent(String student) {
        //先转换为list
        List<String > my_students=new ArrayList<>();
        //再执行删除操作
        my_students.remove(student);
        //把list转为字符串数组
        students=my_students.toArray(new String[my_students.size()]);
        numberOfStudents--;

    }

    public void clear(){
        //删除所有学生
        students=new String[0];
        numberOfStudents=0;
    }

    public String[] getStudents() {
        return students;
    }

    public int getNumberOfStudents() {
        return numberOfStudents;
    }

    public String getCourseName() {
        return courseName;
    }
}
